/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-5
 * @Time  : 下午3:30
 * @brief :
 */
(function () {
    var util = require('common:widget/ui/libs/util.js').util,
        $page = $(".page-assetsdemand"),
        $construct = $page.find('.construct'),
        $profit = $page.find('.profit');

    P.createPage({
        _data:{
           pid:1
        },

        _create:function(){
//            this.createSpline();
            this.createSummary();
            this.createProfit();
        },


        createProfit:function(){
            var me = this;
            $.getJSON('/user/userprofitlist',{
                pid:me.data('pid')
            },function(ret,status){
                var data = [];
                if(ret.error_no == 0){
                    var date = [],
                        list = [];
                    $.each(ret.data,function(k,v){
                        date.push(util.toTime(parseFloat(v.date)*1000,'MM-dd'));
                        list.push(parseFloat(v.profit));
                    });
                    me.createSpline(date,list);
                }
            })
        },

        createSummary:function(){
            var me = this;
            $.getJSON('/user/userfundsummary',{
                pid:me.data('pid')
            },function(ret,status){
                if(ret.error_no == 0){
                    var profit = ret.data.profit,
                        waiting_amount = ret.data.waiting_amount,
                        bought_amount = ret.data.bought_amount,
                        freeze = ret.data.freeze,
                        total_profit = ret.data.total_profit,
                        yesterday_profit = ret.data.yesterday_profit,
                        total = ret.data.total;
                    $construct.find('.now-profit').text('￥'+mathUtil.formatAmount(profit,4));
                    $construct.find('.waiting-amount').text('￥'+mathUtil.formatAmount(waiting_amount,4));
                    $construct.find('.bought-amount').text('￥'+mathUtil.formatAmount(bought_amount,4));
                    $construct.find('.freeze').text('￥'+mathUtil.formatAmount(freeze,4));
                    $profit.find('.total-profit').text('￥'+mathUtil.formatAmount(total_profit,4));
                    $profit.find('.yesterday-profit').text('￥'+mathUtil.formatAmount(yesterday_profit,4));
                    if(bought_amount>0){
                        $construct.find('.assemble').show();
                    }
                    me.createCycle({
                        total:total,
                        bought_amount:bought_amount,
                        profit:profit,
                        waiting_amount:waiting_amount,
                        freeze:freeze
                    });
                }
            })
        },


        createSpline:function(date,list){
            $page.find('.profit-list').highcharts({
               chart:{
                    width:600,
                    height:280,
                    backgroundColor:'#fafafa'
                },
                credits : {
                    enabled:false
                },
                title: {
                    text: ' ',
                    x: -20 //center
                },
                legend: {
                    align: 'center',
                    verticalAlign: 'top',
                    x: 200,
                    y: 0
                },
                xAxis: {
                    type:'datetime',
                    categories: date,
                    gridLineWidth: 1 ,//图形的背景线设置
                    lineWidth : 1 //Y轴的宽度
                },
                yAxis: {
                    gridLineWidth: 1 ,//图形的背景线设置
                    lineWidth : 1, //Y轴的宽度
                    title: {
                        text: '每日收益金额'
                    },
                    // categories : [0,10,20]
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    valuePrefix: '￥'
                },
                plotOptions : {
                    series: {
                        fillOpacity: 0.2
                    }
                },
                series: [{
                    type : 'line',
                    name: '我的收益',
                    data: list,
                    color:'#F93'
                }]
            });
        },


        createCycle:function(data){
            $page.find('.assets-cycle').highcharts({
                chart : {
                    backgroundColor:'rgba(0,0,0,0)',
                    borderColor:"#AAA"
                },

                credits : {
                    enabled:false
                },

                title: {
                    userHTML : true,
                    text: '<div style="color:#669;font-style:italic;font-size:30px">'+'￥' + mathUtil.formatAmount(data.total,4)+ '</div>',
                    color:'#FFF',
                    align: 'center',
                    verticalAlign: 'middle',
                    y: -10
                },
                subtitle : {
                    userHTML : true,
                    text: '<div style="font-size:14px;padding-top:30px;color:#336">资金总额</div>',
                    verticalAlign:'middle',
                    y:20
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                legend: {
                    borderColor: '#909090'
                },
                plotOptions: {
                    pie: {
                        borderWidth:"0px",
                        size:"75%",
                        dataLabels: {
                            enabled: true
                        }
                    }
                },

                series: [{
                    type: 'pie',
                    name: '占比',
                    innerSize: '100%',
                    data :[
                        {
                            name:'购买资金',
                            y:parseFloat(data.bought_amount),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#f93'
                        },
                        {
                            name:'收益资金',
                            y:parseFloat(data.profit),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#fec284'
                        },
                        {
                            name:'排队资金',
                            y:parseFloat(data.waiting_amount),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#ffead9'
                        },
                        {
                            name:'提现中',
                            y:parseFloat(data.freeze),
                            dataLabels : {
                                enabled:false
                            },
                            color:'#ffac5c'
                        }
                    ]
                }]
            });
        }
    });
})();